package cn.tedu.cake.service.impl;

import cn.tedu.cake.ex.ServiceException;
import cn.tedu.cake.mapper.CategoryMapper;
import cn.tedu.cake.pojo.dto.CategoryDTO;
import cn.tedu.cake.pojo.entity.Category;
import cn.tedu.cake.service.ICategoryService;
import cn.tedu.cake.web.JsonResult;
import cn.tedu.cake.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper mapper;
    @Override
    public void addNew(CategoryDTO categoryDTO){
        String name = categoryDTO.getName();
        int c=mapper.countByName(name);
        if (c>0){
            String message="添加分类失败,分类名已存在!";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        mapper.insert(category);
    }

    @Override
    public void deleteById(int id){
        int c = mapper.deleteById(id);
        if (c==0){
            String message = "删除失败,ID不存在!";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
    }

    @Override
    public void update(Category category){
        int c = mapper.update(category);

    }
}
